package com.zjq.authoritysystem.controller.department;

import com.zjq.authoritysystem.entity.Department;
import com.zjq.authoritysystem.service.DepartmentService;
import com.zjq.authoritysystem.vo.query.DepartmentQueryVo;
import com.zjq.authoritysystem.vo.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "部门管理器")
@RestController
@RequestMapping("/api/department")
public class DepartmentController {


    @Resource
    private DepartmentService departmentService;


    @ApiOperation(value = "查询部门列表")
    @GetMapping("/list")
    public Result list(DepartmentQueryVo departmentQueryVo) {
        List<Department> departmentList = departmentService.findDepartmentList(departmentQueryVo);
        return Result.SUCCESS(departmentList);
    }

    @ApiOperation(value = "获取父级部门")
    @GetMapping("/getParentDepartment")
    public Result getParentDepartment() {
        List<Department> list = departmentService.getParentDepartment();
        return Result.SUCCESS(list);
    }

    @ApiOperation(value = "添加部门")
    @PostMapping("/add")
    public Result add(@RequestBody Department department) {
        if (!departmentService.saveOrUpdate(department)) {
            return Result.Error("操作失败");
        }
        return Result.SUCCESS("操作成功");
    }

    @ApiOperation(value = "删除部门")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        if (!departmentService.deleteById(id)) {
            return Result.Error("删除失败");
        }

        return Result.SUCCESS("删除成功");
    }

    @ApiOperation(value = "修改部门信息")
    @PostMapping("/update")
    public Result update(@RequestBody Department department) {
        if (!departmentService.updateById(department)) {
            return Result.Error("修改失败");
        }
        return Result.SUCCESS("修改成功");
    }

    @ApiOperation(value = "检查部门下是否有子部门")
    @PostMapping("/check/{id}")
    public Result check(@PathVariable Integer id){
        List<Department> departmentList = departmentService.getByParentId(id);
        if (departmentList.size()>0){
            return Result.Error(    "该部门下存在子部门，无法删除");
        }
        return Result.SUCCESS();
    }

}
